/**
 * @(#) TrainingMediator.as
 *
 * @date		2009. 04. 03.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	교육 신청 - Mediator
 * 				교육을 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.biztrip_nos.view 
{
	import com.skt.common_hr.biztrip_nos.BizTripFacade;
	import com.skt.common_hr.biztrip_nos.events.BizTripEvent;
	import com.skt.common_hr.biztrip_nos.events.ItemClickEvent;
	import com.skt.common_hr.biztrip_nos.model.BizTripListProxy;
	import com.skt.common_hr.biztrip_nos.model.DailyBizTripProxy;
	import com.skt.common_hr.biztrip_nos.model.NoteUrlProxy;
	import com.skt.common_hr.biztrip_nos.model.vo.BizTripListVO; 
	import com.skt.common_hr.biztrip_nos.view.components.BizTripOverViewNos;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class BizTripMediator extends OverViewMediator
	{
		public static const NAME:String = "TrainingMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function BizTripMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			setAuthority();
 			setDateRange();
 			super.initializeView();

			view.onModifyBizTrip = onModifyBizTrip;
 		}
 		
 		override protected function activatedView():void
 		{
			view.vsBizTrip.selectedIndex = 0;
			loadBizTripList();
			//set event handlers...
 			setEventListeners();
 		}
		

		/**
		* View에 표현된 데이터들을 삭제한다.
		*	- view가 sleep 으로 들어갈때 실행됨..
		* @param
		* @return void
		*/
		override protected function closedView():void
		{
			trace("BizTrip. closeView");
			view.vsBizTrip.selectedIndex = 0;
			//removeEventListeners();
		} 		
		
 		private function setEventListeners():void
 		{
			view.addEventListener(BizTripEvent.REQ_DAILY_BIZTRIP, onReqDailyBizTrip);	// 상시출장
			view.addEventListener(BizTripEvent.REQ_NEAR_BIZTRIP, onReqNearBizTrip);		// 시내출장
			view.addEventListener(BizTripEvent.REQ_SITE_BIZTRIP, onReqSiteBizTrip);		// 현장방문
			view.addEventListener(BizTripEvent.REQ_DOMESTIC_BIZTRIP, onReqDomesticBizTrip);
			view.addEventListener(BizTripEvent.REQ_FOREIGN_BIZTRIP, onReqForeignBizTrip);
			view.addEventListener(BizTripEvent.LOAD_BIZTRIP_LIST, onLoadBizTripList);
 		}
 		
 		private function removeEventListeners():void
 		{
			view.removeEventListener(BizTripEvent.REQ_DAILY_BIZTRIP, onReqDailyBizTrip);
			view.removeEventListener(BizTripEvent.REQ_NEAR_BIZTRIP, onReqNearBizTrip);		// 시내출장
			view.removeEventListener(BizTripEvent.REQ_SITE_BIZTRIP, onReqSiteBizTrip);		// 현장방문
			view.removeEventListener(BizTripEvent.REQ_DOMESTIC_BIZTRIP, onReqDomesticBizTrip);
			view.removeEventListener(BizTripEvent.REQ_FOREIGN_BIZTRIP, onReqForeignBizTrip);
			view.removeEventListener(BizTripEvent.LOAD_BIZTRIP_LIST, onLoadBizTripList);
 		}
 		
 		private function setAuthority():void
 		{
			view.btnDailyTrip.visible = false;
			view.btnDomestic.visible = false;
			view.btnForeign.visible = false;
 			
			for each(var auth:* in Variables.authorities) 
			{
				//if(auth.EMENU == "REGULARTRIP") {
					view.btnDailyTrip.visible = true;
				//}	
				if(auth.EMENU == "DOMESTICTRIP") {
					view.btnDomestic.visible = true;
				}	
				if(auth.EMENU == "OVERSEASTRIP") {
					view.btnForeign.visible = true;
				}	
			} 			
 		}
 		


 		private function loadBizTripList():void
 		{
 			var listProxy:BizTripListProxy = facade.retrieveProxy(BizTripListProxy.NAME) as BizTripListProxy;
 			listProxy.load(Variables.myPernr, view.BEGDA, view.ENDDA);
 		}
 		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat( [
					BizTripFacade.LOAD_BIZTRIP_LIST_COMPLETE,
					BizTripFacade.LOAD_BIZTRIP_LIST_FAILED,
					BizTripFacade.DELETE_BIZTRIP_COMPLETE,
					BizTripFacade.DELETE_BIZTRIP_FAILED,
					BizTripFacade.CHANGE_VIEW_BIZTRIP_LIST,
					BizTripFacade.LOAD_NOTE_URL_COMPLTE,
					BizTripFacade.LOAD_NOTE_URL_FAILED
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case BizTripFacade.LOAD_BIZTRIP_LIST_COMPLETE:	// 
					view.dataProvider = (note.getBody() as BizTripListProxy).bizTripList;
					//view.referFile01 =  (note.getBody() as BizTripListProxy).url01;
					//view.referFile02 =  (note.getBody() as BizTripListProxy).url02;
					trace("bundle: " + commonFacade.bundle);
					commonFacade.setStatusMessage("STATUS_LOAD_BIZTRIP_LIST", [view.dataProvider.length]);
					break;
				case BizTripFacade.LOAD_BIZTRIP_LIST_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_LOAD_LIST", null, "TITLE_SYSTEM_ERROR");
					break;
				case BizTripFacade.DELETE_BIZTRIP_COMPLETE:
					loadBizTripList();
					break;
				case BizTripFacade.DELETE_BIZTRIP_FAILED:	// error delete
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_DELETE_LIST", null, "TITLE_SYSTEM_ERROR", view); 
					break;
				case BizTripFacade.CHANGE_VIEW_BIZTRIP_LIST:
					view.changeView(BizTripOverViewNos.BIZTRIP_LIST_VIEW);
					loadBizTripList();
					break;
				case BizTripFacade.LOAD_NOTE_URL_COMPLTE:
					onNotePrint((note.getBody() as NoteUrlProxy).url);
					break;
				case BizTripFacade.LOAD_NOTE_URL_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "("+(note.getBody() as NoteUrlProxy).errCode+")"+ (note.getBody() as NoteUrlProxy).errText);
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
		
		
 
 
 
 
 
 
 
 
 
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		// 시내출장
  		private function onReqNearBizTrip(event:Event):void
 		{
 			commonFacade.setStatusMessage("");
 			view.changeView(BizTripOverViewNos.BIZTRIP_NEAR_VIEW);
 			if(facade.retrieveMediator(NearMediator.NAME)==null) {
 				facade.registerMediator(new NearMediator(view.nearView));
 			}
 			facade.sendNotification(BizTripFacade.MODIFY_NEAR_VIEW, null);	 
 			
 		}
 		
 		// 상시출장
  		private function onReqDailyBizTrip(event:Event):void
 		{
 			commonFacade.setStatusMessage("");
 			view.changeView(BizTripOverViewNos.BIZTRIP_DAILY_VIEW);
 			if(facade.retrieveMediator(DailyMediator.NAME)==null) {
 				facade.registerMediator(new DailyMediator(view.dailyView));
 			}
 			facade.sendNotification(BizTripFacade.MODIFY_DAILY_VIEW, null);	 
 			
 		}

 		// 현장방문
  		private function onReqSiteBizTrip(event:Event):void
 		{
 			commonFacade.setStatusMessage("");
 			view.changeView(BizTripOverViewNos.BIZTRIP_SITE_VIEW);
 			if(facade.retrieveMediator(SiteMediator.NAME)==null) {
 				facade.registerMediator(new SiteMediator(view.siteView));
 			}
 			facade.sendNotification(BizTripFacade.MODIFY_SITE_VIEW, null);	 
 			
 		}
 		private function onReqDomesticBizTrip(event:Event):void
 		{
  			commonFacade.setStatusMessage("");
			view.changeView(BizTripOverViewNos.BIZTRIP_DOMESTIC_VIEW);
 			if(facade.retrieveMediator(DomesticMediator.NAME)==null) {
 				facade.registerMediator(new DomesticMediator(view.domesticView));
 			}
 			facade.sendNotification(BizTripFacade.MODIFY_DOMESTIC_VIEW, null);
 		}
 		private function onReqForeignBizTrip(event:Event):void
 		{
 			commonFacade.setStatusMessage("");
 			view.changeView(BizTripOverViewNos.BIZTRIP_FOREIGN_VIEW);
 			if(facade.retrieveMediator(ForeignMediator.NAME)==null) {
 				facade.registerMediator(new ForeignMediator(view.foreignView));
 			}
 			facade.sendNotification(BizTripFacade.MODIFY_FOREIGN_VIEW, null);
 		}
 		
 		private function onLoadBizTripList(evnet:BizTripEvent):void
 		{
 			loadBizTripList();
 		}
 		
 		private function onModifyBizTrip(event:ItemClickEvent):void
 		{
 			switch(event.command)
 			{
 			case ItemClickEvent.ITEM_READONLY:
 				readBizTrip(event.item);
 				break;
 			case ItemClickEvent.ITEM_MODIFY:
 				modifyBizTrip(event.item);
	 			break;
 			case ItemClickEvent.ITEM_DELETE:
 				CommonAlert.showMsgYesNo(commonFacade.bundle,"ALERT_PROMPT_DELETE", null, "ALERT_TITLE_DEFAULT", view,
 								function():void{deleteBizTrip(event.item)}, null);
				//deleteBizTrip(event.item); 			
 				break;
 			case ItemClickEvent.ITEM_CANCEL:
 				CommonAlert.showMsgYesNo(commonFacade.bundle,"ALERT_PROMPT_DELETE", null, "ALERT_TITLE_DEFAULT", view,
 								function():void{deleteBizTrip(event.item)}, null);
 				//deleteBizTrip(event.item);
 				break;
 			case ItemClickEvent.ITEM_RETRY:
 			case ItemClickEvent.ITEM_ADJUST:
 				modifyBizTrip(event.item);
 				break;
 			case ItemClickEvent.ITEM_PRINT:
 				printBizTrip(event.item);
 				break;
 		
 			}
 		}
 		
 		private function readBizTrip(item:BizTripListVO):void
 		{
 			switch(item.TRTYPE) {
 				case BizTripFacade.TYPE_DAILY_BIZTRIP:
 					commonFacade.setStatusMessage("");
		 			view.changeView(BizTripOverViewNos.BIZTRIP_DAILY_VIEW);
		 			if(facade.retrieveMediator(DailyMediator.NAME)==null) {
		 				facade.registerMediator(new DailyMediator(view.dailyView));
		 			}
		 			facade.sendNotification(BizTripFacade.READONLY_DAILY_VIEW, item);
 					break;
 				case BizTripFacade.TYPE_NEAR_BIZTRIP:
 					commonFacade.setStatusMessage("");
		 			view.changeView(BizTripOverViewNos.BIZTRIP_NEAR_VIEW);
		 			if(facade.retrieveMediator(NearMediator.NAME)==null) {
		 				facade.registerMediator(new NearMediator(view.nearView));
		 			}
		 			facade.sendNotification(BizTripFacade.READONLY_NEAR_VIEW, item);
 					break;
 				case BizTripFacade.TYPE_SITE_BIZTRIP:
 					commonFacade.setStatusMessage("");
		 			view.changeView(BizTripOverViewNos.BIZTRIP_SITE_VIEW);
		 			if(facade.retrieveMediator(SiteMediator.NAME)==null) {
		 				facade.registerMediator(new SiteMediator(view.siteView));
		 			}
		 			facade.sendNotification(BizTripFacade.READONLY_SITE_VIEW, item);
 					break;
 				case BizTripFacade.TYPE_DOMESTIC_BIZTRIP:
 					commonFacade.setStatusMessage("");
		 			view.changeView(BizTripOverViewNos.BIZTRIP_DOMESTIC_VIEW);
		 			if(facade.retrieveMediator(DomesticMediator.NAME)==null) {
		 				facade.registerMediator(new DomesticMediator(view.domesticView));
		 			}
		 			facade.sendNotification(BizTripFacade.READONLY_DOMESTIC_VIEW, item);
 					break;
 				case BizTripFacade.TYPE_FOREIGN_BIZTRIP:
 					commonFacade.setStatusMessage("");
		 			view.changeView(BizTripOverViewNos.BIZTRIP_FOREIGN_VIEW);
		 			if(facade.retrieveMediator(ForeignMediator.NAME)==null) {
		 				facade.registerMediator(new ForeignMediator(view.foreignView));
		 			}
		 			facade.sendNotification(BizTripFacade.READONLY_FOREIGN_VIEW, item);
 					break;
 			}
 		}

 		private function modifyBizTrip(item:BizTripListVO):void
 		{
 			switch(item.TRTYPE) {
 				case BizTripFacade.TYPE_DAILY_BIZTRIP:
		 			view.changeView(BizTripOverViewNos.BIZTRIP_DAILY_VIEW);
		 			if(facade.retrieveMediator(DailyMediator.NAME)==null) {
		 				facade.registerMediator(new DailyMediator(view.dailyView));
		 			}
		 			facade.sendNotification(BizTripFacade.MODIFY_DAILY_VIEW, item);
 					break;
 				case BizTripFacade.TYPE_NEAR_BIZTRIP:
		 			view.changeView(BizTripOverViewNos.BIZTRIP_NEAR_VIEW);
		 			if(facade.retrieveMediator(NearMediator.NAME)==null) {
		 				facade.registerMediator(new NearMediator(view.nearView));
		 			}
		 			facade.sendNotification(BizTripFacade.MODIFY_NEAR_VIEW, item);
 					break;
 				case BizTripFacade.TYPE_SITE_BIZTRIP:
		 			view.changeView(BizTripOverViewNos.BIZTRIP_SITE_VIEW);
		 			if(facade.retrieveMediator(SiteMediator.NAME)==null) {
		 				facade.registerMediator(new SiteMediator(view.siteView));
		 			}
		 			facade.sendNotification(BizTripFacade.MODIFY_SITE_VIEW, item);
 					break;
 				case BizTripFacade.TYPE_DOMESTIC_BIZTRIP:
		 			view.changeView(BizTripOverViewNos.BIZTRIP_DOMESTIC_VIEW);
		 			if(facade.retrieveMediator(DomesticMediator.NAME)==null) {
		 				facade.registerMediator(new DomesticMediator(view.domesticView));
		 			}
		 			facade.sendNotification(BizTripFacade.MODIFY_DOMESTIC_VIEW, item);
 					break;
 				case BizTripFacade.TYPE_FOREIGN_BIZTRIP:
		 			view.changeView(BizTripOverViewNos.BIZTRIP_FOREIGN_VIEW);
		 			if(facade.retrieveMediator(ForeignMediator.NAME)==null) {
		 				facade.registerMediator(new ForeignMediator(view.foreignView));
		 			}
		 			facade.sendNotification(BizTripFacade.MODIFY_FOREIGN_VIEW, item);
 					break;
 			}
 		}

		private function deleteBizTrip(item:BizTripListVO):void
		{
			var proxy:DailyBizTripProxy = facade.retrieveProxy(DailyBizTripProxy.NAME) as DailyBizTripProxy;
			proxy.delBizTrip(item.TRSTATUS, item.GWAREKEY01, item.GWAREKEY02);
		}

		private var fileRef:FileReference;
        private var urlReq:URLRequest;  
		private function printBizTrip(item:BizTripListVO):void
		{
			var proxy:NoteUrlProxy = facade.retrieveProxy(NoteUrlProxy.NAME) as NoteUrlProxy;
			proxy.load(item.GWAREKEY02);

		}
		
		private function onNotePrint(url:String):void
		{
			var req:URLRequest = new URLRequest("../travel/"+url);
			trace(url);
			navigateToURL(req);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		private function setDateRange():void
 		{
  			var date:Date= DateUtil.todayNoTime();
 			date.month -= 1;
 			date.date = 1;
 			view.BEGDA = new Date(date);
 			date.month += 3;
 			date.date = 1;
 			date.date -= 1;
 			view.ENDDA = new Date(date);
			/*
			var date:Date= DateUtil.todayNoTime();
 			date.month = 0;
 			date.date = 1;
 			view.BEGDA = new Date(date);
 			date.month = 11;
 			date.date = 31;
 			view.ENDDA = new Date(date);
 			*/
 		}
 		
        private function get view():BizTripOverViewNos
        {
            return viewComponent as BizTripOverViewNos;
        }
		
		
	}
}